const MessageService = require('../services/message.service')
const messageService = new MessageService()

const RegistrationService = require('../services/registration.service')
const registrationService = new RegistrationService()
class MessageController {
  async findAll (ctx, next) {
    const query = ctx.request.query
    let regQuery = {
      page: 1,
      pageSize: 999999
    }
    if (ctx.state.user.role === 'reporter') {
      regQuery.submitter = ctx.state.user._id
    } else if (ctx.state.user.role === 'auditor') {
      regQuery.reg_xzxq = ctx.state.user.area
    }
    const registrations = await registrationService.findAll(regQuery)
    query.ref = {
      $in: registrations.data.map(i => i._id)
    }
    const data = await messageService.findAll(query)
    ctx.response.body = data
  }
  async findOne (ctx, next) {
    const id = ctx.request.params.id
    const data = await messageService.findOne(id)
    ctx.response.body = data[0]
  }
  async updateOne (ctx, next) {
    const id = ctx.request.params.id
    const body = ctx.request.body
    const data = await messageService.updateOne(id, body)
    ctx.response.body = data
  }
  async deleteOne (ctx, next) {
    const id = ctx.request.params.id
    const data = await messageService.deleteOne(id)
    ctx.response.body = data
  }
}

module.exports = MessageController